package cn.wisenergy.pai.monitor.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;


public class ConfigurationTools {

	private static String APP_CONFIGURATION_FILE = "/conf/pai.conf";

	
	private static String JOB_SERVER_CONFIGURATION_FILE = "/conf/server.properties";
	
	public static String getPAIHOME(){
		String pai_home=System.getProperty("PAI_HOME");
		if(pai_home==null||pai_home.length()==0){
			pai_home=System.getenv("PAI_HOME");
		}
		return pai_home;
	}

	public static String getAppConfiguration(String key) throws IOException {
		
		File log4j=new File(getPAIHOME()+APP_CONFIGURATION_FILE);
		return getConfiguration(key,log4j);
	}
	
	public static String getPAIConfValue(String key) throws IOException {
		String value = getAppConfiguration(key);
		if(value!=null)
		{
			String[] tmp = value.trim().split("#");
			if(tmp!=null && tmp.length > 0 && tmp[0]!=null)
				return tmp[0].trim();
		}
		return value;
	}
	
	public static String getServerConfiguration(String key) throws IOException
	{
		File file = new File(getPAIHOME() + JOB_SERVER_CONFIGURATION_FILE);
		return getConfiguration(key, file);
	}
	
	
	
	private static String getConfiguration(String key,File log4j) throws IOException{
		FileInputStream is= new FileInputStream(log4j);
		Properties p=new Properties();
		try{
			p.load(is);
		}finally{
			is.close();
		}
		return p.getProperty(key);
	}
}
